﻿using Common.Contract;
using Common.Contract.Customer.Identifier;
using Common.Contract.Customer.Record;
using Common.Contract.DataCenter.Record;
using Common.Contract.DataCenter.Request;
using Common.Contract.SaleOut.Record;
using Common.Service.Command;
using Common.Service.Queue;
using Common.Util;
using DataCenter.Configuration;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataCenter.ServiceCommand
{
    public class UpdateRdcHistoryServiceCommand : ProviderQueueCommand<IProviderFactory, RdcHistRecord, ResponseRecord>
    {
        public UpdateRdcHistoryServiceCommand(IProviderFactory provider) : base(provider) { }
        protected override void Validate(RdcHistRecord record)
        {
            if (record == null) throw new ArgumentNullException("request");
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var rdcHist = Record;
            var obj = new SerialNumberRdcHist()
            {
                BoxNo = rdcHist.BoxNumber,
                CreateDate = rdcHist.CreateDate,
                EdiNo = rdcHist.EDINumber,
                IncentiveDate = rdcHist.IncentiveDate,
                IncentiveFlag = rdcHist.IncentiveFlag.HasValue ? rdcHist.IncentiveFlag.Value.ToBooleanString() : null,
                InvOrg = rdcHist.InventoryOrganization,
                LineNo = rdcHist.LineNumber,
                Model = rdcHist.Model,
                ModelSuffix = rdcHist.ModelSuffix,
                OutDate = rdcHist.OutDate,
                SelloutDate = rdcHist.SelloutDate,
                SelloutStatus = rdcHist.SelloutStatus,
                SerialNo = rdcHist.SerialNumber,
                ShipNo = rdcHist.ShipNumber,
                SmsYn = rdcHist.SmsFlag.HasValue ? rdcHist.SmsFlag.Value.ToBooleanString() : null,
                SoTransferDate = DateTime.Now,
                SoTransferFlag = BooleanString.TRUE,
                Suffix = rdcHist.Suffix,
                WtIfDate = rdcHist.WarrantyInfoDate,
                WtIfFlag = rdcHist.WarrantyInfoFlag.HasValue ? rdcHist.WarrantyInfoFlag.Value.ToBooleanString() : null,
            };

            var itemCheck = Providers.SerialNumberRdcHist.Retrieve(Record.Model, Record.Suffix, Record.SerialNumber);
            if (itemCheck == null)
            {
                var resultitem1 = Providers.SerialNumberRdcHist.Add(obj);
            }
            else
            {
                var resultitem2 = Providers.SerialNumberRdcHist.Update(obj);
            }
            return true;
        }
    }
}
